package com.gwnet.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.common.core.exception.base.BaseException;
import com.gwnet.common.satoken.utils.LoginHelper;
import com.gwnet.common.util.Arith;
import com.gwnet.rider.domain.RiderWallet;
import com.gwnet.rider.domain.RiderWithdrawCash;
import com.gwnet.rider.domain.bo.ApplyWithdrawalBO;
import com.gwnet.rider.domain.bo.RiderWithdrawCashPageBO;
import com.gwnet.rider.enums.AuditStatusEnum;
import com.gwnet.rider.enums.WithdrawalTypeEnum;
import com.gwnet.rider.mapper.RiderWithdrawCashMapper;
import com.gwnet.rider.service.RiderWalletService;
import com.gwnet.rider.service.RiderWithdrawCashService;
import com.gwnet.system.api.model.LoginUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 骑手提现申请表 服务实现类
 * </p>
 *
 * @author WangLiang
 * @since 2024-08-06
 */
@Service
@AllArgsConstructor
public class RiderWithdrawCashServiceImpl extends ServiceImpl<RiderWithdrawCashMapper, RiderWithdrawCash> implements RiderWithdrawCashService {
    /** 最小提现金额，后续要改成平台设置 */
    private final static int MIN_WITHDRAWAL_MONEY = 100;

    private final RiderWithdrawCashMapper withdrawCashMapper;
    private final RiderWalletService riderWalletService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean applyWithdrawal(ApplyWithdrawalBO withdrawalBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        // 查询个人账户金额
        RiderWallet riderWallet = riderWalletService.getById(loginUser.getUserId());

        check(riderWallet,withdrawalBO);

        // TODO 人工提现提现分支
        if(WithdrawalTypeEnum.MANUAL.code().equals(withdrawalBO.getWithdrawalType())){

        }

        // 生成提现记录
        RiderWithdrawCash cash = new RiderWithdrawCash();
        cash.setRiderId(loginUser.getUserId());
        cash.setAmount(withdrawalBO.getWithdrawalAmount());
        cash.setStatus(AuditStatusEnum.ING.code());
        cash.setRiderRemark(withdrawalBO.getRemark());
        cash.setPayType(withdrawalBO.getPayType());
        cash.setWithdrawalType(withdrawalBO.getWithdrawalType());
        int count = withdrawCashMapper.insert(cash);

        // 账户扣款 待结算金额扣款，计算剩余金额
        double remainingAmount = Arith.sub(riderWallet.getSettledAmount(), withdrawalBO.getWithdrawalAmount());
        riderWallet.setSettledAmount(remainingAmount);
        riderWalletService.updateById(riderWallet);


        // TODO 执行具体打款操作



        return count > 0;
    }

    @Override
    public IPage<RiderWithdrawCash> page(RiderWithdrawCashPageBO pageBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Page<RiderWithdrawCash> page = Page.of(pageBO.getCurrent(), pageBO.getSize());
        LambdaQueryWrapper<RiderWithdrawCash> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RiderWithdrawCash::getRiderId,loginUser.getUserId());

        // 状体筛选条件
        if(ObjectUtils.isNotEmpty(pageBO.getPayType())){
            queryWrapper.eq(RiderWithdrawCash::getPayType,pageBO.getPayType());
        }
        if(ObjectUtils.isNotEmpty(pageBO.getWithdrawalType())){
            queryWrapper.eq(RiderWithdrawCash::getWithdrawalType,pageBO.getWithdrawalType());
        }
        if(ObjectUtils.isNotEmpty(pageBO.getStatus())){
            queryWrapper.eq(RiderWithdrawCash::getStatus,pageBO.getStatus());
        }
        // 排序
        queryWrapper.orderByDesc(RiderWithdrawCash::getCreateTime);

        Page<RiderWithdrawCash> pageResult = withdrawCashMapper.selectPage(page, queryWrapper);
        return pageResult;
    }

    /**
     * 前置检查
     * @param riderWallet
     * @param withdrawalBO
     */
    private void check(RiderWallet riderWallet,ApplyWithdrawalBO withdrawalBO){

        // 系统提现
        if(WithdrawalTypeEnum.SYSTEM.code().equals(withdrawalBO.getWithdrawalType())){
            // 申请金额是否小于100元
            if(withdrawalBO.getWithdrawalAmount() < MIN_WITHDRAWAL_MONEY){
                throw new BaseException("申请提现金额不能小于100元");
            }
            // 个人账户可提现金额是否大于100元
            if(riderWallet.getSettledAmount() < MIN_WITHDRAWAL_MONEY){
                throw new BaseException("您的账户中不足100元，无法申请提现");
            }
            // 人工提现
        } else if(WithdrawalTypeEnum.MANUAL.code().equals(withdrawalBO.getWithdrawalType())){
            if(riderWallet.getSettledAmount() < withdrawalBO.getWithdrawalAmount()){
                throw new BaseException("提款金额不能大于您的余额");
            }
        }
    }
}
